﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;
using System.ComponentModel;
using TestApplication.business.src;
using TestApplication.usercontrols;
using TestApplication.control;
using TestApplication.ViewModels;
using TestApplication.business.src.Types;
using System.Collections;
using System.IO;
using System.Drawing.Imaging;

namespace TestApplication
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public PluginView pluginView;
        public MainWindow()
        {
            InitializeComponent();
            //this.myGrid.Height = System.Windows.SystemParameters.VirtualScreenHeight;
            this.WindowState = WindowState.Maximized;
            this.WindowStyle = WindowStyle.None;
            this.Background = System.Windows.Media.Brushes.Black;
          
            this.Closing += new CancelEventHandler(MainWindow_Closing);
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
            this.PreviewKeyDown += new KeyEventHandler(MainWindow_PreviewKeyDown);
        }

        void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if ((e.Key == Key.X) && (Keyboard.Modifiers == ModifierKeys.Control))
                this.Close();
        }

        
        public void InitializePluginButtons(AvailablePlugins pluginList)
        {
            for (int i = 0; i < (pluginList.Count + 1) / 2; i++)
            {
                ColumnDefinition columnDef = new ColumnDefinition();
                columnDef.Width = GridLength.Auto;
                this.PluginSelectionGrid.ColumnDefinitions.Add(columnDef);
            }


            int row = 0;
            int column = 0;

            IEnumerator enumerator = pluginList.GetEnumerator();
            while (enumerator.MoveNext())
            {
                AvailablePlugin plugin = (AvailablePlugin)enumerator.Current;
                Button button = new Button();
                MemoryStream ms = new MemoryStream();
                plugin.Instance.GetPluginImage().Save(ms, ImageFormat.Jpeg);
                BitmapImage bImg = new BitmapImage();
                bImg.BeginInit();
                bImg.StreamSource = new MemoryStream(ms.ToArray());
                bImg.EndInit();
                button.Foreground = new ImageBrush(bImg);
                button.Background = new ImageBrush(bImg);
                button.Content = plugin.Instance.Name;
                button.Width = 200;
                button.Click += new RoutedEventHandler(button_Click);
                button.Height = 150;
                button.Margin = new Thickness(50);
                this.PluginSelectionGrid.Children.Add(button);
                Grid.SetRow(button, row);
                Grid.SetColumn(button, column);
                column++;
                if (column >= (pluginList.Count+1) / 2)
                {
                    column = 0;
                    row = 1;
                }
            }
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            CaptureModel.Instance.View = this;
        }

        void button_Click(object sender, RoutedEventArgs e)
        {
            String pluginName = (String)((Button)sender).Content;
            this.MainGrid.Children.Clear();
            this.pluginView = new PluginView();
            this.MainGrid.Children.Add(pluginView);
            CaptureModel.Instance.CurrentRunningPlugin = pluginName;
            CaptureModel.Instance.InitializePlugin();
            CaptureModel.Instance.StartWebcam();
        }

        public void GoBack()
        {
            this.MainGrid.Children.Remove(this.pluginView);
            this.MainGrid.Children.Add(this.PluginSelectionGrid);
        }

        void MainWindow_Closing(object sender, CancelEventArgs e)
        {
            Environment.Exit(0);
        }
    }
}
